function tj() {
    var shuliang = document.getElementById("shuliangText").value;
    if(shuliang == ''){
        alert("输入数量！");
        return false;
    }
    var danjia = document.getElementById("danjiaText").value;
    if(danjia == ''){
        alert("输入单价！");
        return false;
    }
    var jine = document.getElementById("jineText").value;
    if(jine == ''){
        alert("输入金额！");
        return false;
    }
    var zhangshu = document.getElementById("zhangshuText").value;
    // if(zhangshu == ''){
    //     alert("输入张数！");
    //     return false;
    // }
    var postParam = {
        'shuliang':shuliang,
        'danjia':danjia,
        'jine':jine,
        'zhangshu':zhangshu,
    };

    //提交数据
    $.ajax({
        type: "post",
        url: '/invoice/invoiceSplit/api',
        data:postParam,
        dataType: 'json',
        success: function (data) {
            $("#tables tbody").html("");
            debugger;
            if(data.code != undefined && data.code == 1){
                alert("计算错误：" + data.msg);
                return false;
            }
            if(data == undefined || data == null || data.length == 0){
                alert("计算错误，请检查输入！");
                return false;
            }
            $("tbody").show();
            debugger;
            for(var v = 0;v < data.length;v++){
                var d = data[v];
                $("tbody").append('<tr style="background-color: #ffffff; height: 25px;"><td>'+(v+1)+'</td><td>'+d.shuliang+'</td><td>'+d.danjia.toFixed(2)+'</td><td>'+d.shuilv+'</td><td>'+d.jine.toFixed(2)+'</td></tr>')
            }
        }
    });
}

function jsje() {
    var shuliang = document.getElementById("shuliangText");
    validationNumber(shuliang,2);
    var danjia = document.getElementById("danjiaText");
    validationNumber(danjia,2);
    document.getElementById("jineText").value = mul(mul(danjia.value,shuliang.value),1.16).toFixed(2);
}

function validationNumber(e, num) {
    var regu = /^[0-9]+\.?[0-9]*$/;
    if (e.value != "") {
        if (!regu.test(e.value)) {
            alert("请输入正确的数字");
            e.value = e.value.substring(0, e.value.length - 1);
            e.focus();
        } else {
            if (num == 0) {
                if (e.value.indexOf('.') > -1) {
                    e.value = e.value.substring(0, e.value.length - 1);
                    e.focus();
                }
            }
            if (e.value.indexOf('.') > -1) {
                if (e.value.split('.')[1].length > num) {
                    e.value = e.value.substring(0, e.value.length - 1);
                    e.focus();
                }
            }

        }
    }
}
/**
 ** 乘：解决相乘失去精度，得到小数点显示很多位
 **/
function mul(arg1, arg2) {
    var m = 0, s1 = arg1.toString(), s2 = arg2.toString();
    try {
        m += s1.split(".")[1].length;
    }
    catch (e) {
    }
    try {
        m += s2.split(".")[1].length;
    }
    catch (e) {
    }
    return Number(s1.replace(".", "")) * Number(s2.replace(".", "")) / Math.pow(10, m);
}

/**
 ** 导出Excel
 **/
var tableToExcel = (function () {
    var uri = 'data:application/vnd.ms-excel;base64,',
        template = '<html xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:x="urn:schemas-microsoft-com:office:excel" xmlns="http://www.w3.org/TR/REC-html40"><head><!--[if gte mso 9]><xml><x:ExcelWorkbook><x:ExcelWorksheets><x:ExcelWorksheet><x:Name>{worksheet}</x:Name><x:WorksheetOptions><x:DisplayGridlines/></x:WorksheetOptions></x:ExcelWorksheet></x:ExcelWorksheets></x:ExcelWorkbook></xml><![endif]--></head><body><table>{table}</table></body></html>',
        base64 = function (s) { return window.btoa(unescape(encodeURIComponent(s))) },
        format = function (s, c) { return s.replace(/{(\w+)}/g, function (m, p) { return c[p]; }) };
    return function (table, name, filename) {
        if (!table.nodeType) table = document.getElementById(table)
        var ctx = { worksheet: name || 'Worksheet', table: table.innerHTML }

        document.getElementById("dlink").href = uri + base64(format(template, ctx));
        document.getElementById("dlink").download = filename;
        document.getElementById("dlink").click();
    }
})()